import subprocess
import sys
import time

# print(f"当前执行用例的目录：", os.getcwd())
path_list = [
    r"D:\study\w_code\Tesla\api_frame\tests\yaml_case\bbs",
    r"D:\study\w_code\Tesla\api_frame\tests\yaml_case\ddt",
    r"D:\study\w_code\Tesla\api_frame\tests\yaml_case\weixin",
    r"D:\study\w_code\Tesla\api_frame\tests\yaml_case\加密用例",
    r"D:\study\w_code\Tesla\api_frame\tests\yaml_case\商城用例",
    r"D:\study\w_code\Tesla\api_frame\tests\yaml_case\流程用例",
]

python_path = sys.executable  # 可执行的python
run_path = r"D:\study\w_code\Tesla\api_frame\run_by_django.py"


def start_frame(path, run_result, api_case_count):
    print("开始执行框架：", time.time())

    proc = subprocess.run(
        f"{python_path} {run_path} run_result {run_result.id} {api_case_count}",
        # 子进程执行的结果和我们自己进入到这个目录下执行dir的结果是一样的
        cwd=path,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )

    try:
        print(proc.stdout.decode("utf-8"))
        print(proc.stderr.decode("utf-8"))
    except UnicodeDecodeError:
        print(proc.stdout.decode("gbk"))
        print(proc.stderr.decode("gbk"))
